﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TVShow.Framework
{
  public class INIFile
  {
    #region Kernel32 imports
    [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
        SetLastError = true,
        CharSet = CharSet.Unicode, ExactSpelling = true,
        CallingConvention = CallingConvention.StdCall)]
    private static extern int GetPrivateProfileString(
        string lpAppName,
        string lpKeyName,
        string lpDefault,
        string lpReturnString,
        int nSize,
        string lpFilename);

    [DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW",
        SetLastError = true,
        CharSet = CharSet.Unicode, ExactSpelling = true,
        CallingConvention = CallingConvention.StdCall)]
    private static extern int WritePrivateProfileString(
        string lpAppName,
        string lpKeyName,
        string lpString,
        string lpFilename);
    #endregion

    private string iniFile;

    public INIFile(string fileName) {
      this.iniFile = fileName;
    }

    public Dictionary<string, string> GetValues(string Section) {
      Dictionary<string, string> Values = new Dictionary<string, string>();
      List<string> KeyNames = GetKeys(Section);
      foreach( string key in KeyNames) {
        Values.Add(key, GetString(Section, key, ""));
      }
      return Values;
    }

    public void Save(string Section, Dictionary<string,string> Values) {
      foreach (string keyName in Values.Keys) {
        SaveString(Section, keyName, Values[keyName]);
      }
    }

    /// <summary>
    /// Saves a value to the ini file
    /// </summary>
    /// <param name="Section">Section name (name in brackets)</param>
    /// <param name="Key">Left side of the =</param>
    /// <param name="value">Right side of the =</param>
    public void SaveString(string Section, string key, string value) {
      WritePrivateProfileString(Section, key, value, iniFile);
    }

    /// <summary>
    /// Gets the content.
    /// </summary>
    /// <param name="iniFile">The ini fileName.</param>
    /// <param name="CategoryName">The CategoryName.</param>
    /// <param name="Key">The Key.</param>
    /// <param name="DefaultValue">The default value.</param>
    /// <returns></returns>
    public string GetString(string Section, string Key, string DefaultValue) {
      string returnString = new string(' ', 1024);
      GetPrivateProfileString(Section, Key, DefaultValue, returnString, 1024, iniFile);
      return returnString.Split('\0')[0];
    }

    /// <summary>
    /// Gets the keys.
    /// </summary>
    /// <param name="iniFile">The ini fileName.</param>
    /// <param name="CategoryName">The CategoryName.</param>
    List<string> GetKeys(string Section) {
      string returnString = new string(' ', 32768);
      GetPrivateProfileString(Section, null, null, returnString, 32768, iniFile);
      List<string> result = new List<string>(returnString.Split('\0'));
      result.RemoveRange(result.Count - 2, 2);
      return result;
    }

    /// <summary>
    /// Gets the categories.
    /// </summary>
    /// <param name="iniFile">The ini fileName.</param>
    /// <returns></returns>
    public List<string> GetSections() {
      string returnString = new string(' ', 65536);
      GetPrivateProfileString(null, null, null, returnString, 65536, iniFile);
      List<string> result = new List<string>(returnString.Split('\0'));
      result.RemoveRange(result.Count - 2, 2);
      return result;
    }
  }

}

